home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6201 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  62 lines

  1. Path: news.iadfw.net!usenet
  2. From: alpet@airmail.net (Adam Peterson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I modify a character string that's declared as an array of char?
  5. Date: Fri, 23 Feb 1996 05:14:36 GMT
  6. Organization: customer of Internet America
  7. Message-ID: <4gjbjh$o0h@news-f.iadfw.net>
  8. References: <4gj2nl$840@mirzam.usc.edu>
  9. NNTP-Posting-Host: dal24-12.ppp.iadfw.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. awawda@mirzam.usc.edu (Abu A. Wawda) wrote:
  13.  
  14. >How do I write a function that can modify a string that was declared
  15. >as an array of characters?
  16.  
  17. >for example:
  18.  
  19. >myfunction(char **s)
  20. >{
  21. >    // modify string s
  22. >}
  23.  
  24. >main()
  25. >{
  26. >   char mydata[50];
  27.  
  28. >   strcpy(mydata,"test string");
  29. >   myfunction(&mydata);
  30. >}
  31.  
  32. >this does not seem to work! it works fine if i pass i declare mydata
  33. >as a pointer to a character like this (or dynamically allocate the
  34. >string using a pointer) and pass the address of the pointer:
  35.  
  36. >the problem with this is i need to write a function that will modify a
  37. >string created with an array of characters. i can't figure out how do
  38. >to this. please help. thanks!
  39.  
  40. void myfunction(char *s)
  41. {
  42.     strcat(s,"more stuff");    // modify string s
  43. }
  44.  
  45. void main()
  46. {
  47.    char mydata[50];
  48.  
  49.    strcpy(mydata,"test string");
  50.    printf(mydata);
  51.    myfunction(mydata);
  52.    printf(mydata);
  53. }
  54.  
  55. The above does what I think you want to do.  When you call
  56. 'myfunction', the '&' was not neccessary by definition, then *in*
  57. 'myfunction', you were dropping two levels of redirection down.
  58.  
  59. Adam
  60.  
  61.  
  62.